<template>
  <div class="app-container">
    <el-card style="margin-bottom: 10px">
      <el-row>
        <el-form
          :model="queryParams"
          ref="queryForm"
          :inline="true"
          v-show="showSearch"
          label-width="108px"
        >
          <el-row>
            <el-col :span="6">
              <el-form-item label="合同名称" prop="name">
                <el-input
                  v-model="queryParams.name"
                  placeholder="请输入"
                  clearable
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="合同类型" prop="type" label-width="128px">
                <el-select
                  v-model="queryParams.type"
                  clearable
                  placeholder="请选择"
                  @change="handleQuery"
                >
                  <el-option
                    v-for="item in options"
                    :key="item.value"
                    :label="item.name"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item
                label="合同所属公司"
                prop="belongingCompanyId"
                label-width="128px"
              >
                <el-select
                  v-model="queryParams.belongingCompanyId"
                  clearable
                  placeholder="请选择"
                  @change="
                    handleQuery,
                      getlistContractCategorybyBelongingCompanyId(
                        queryParams.belongingCompanyId
                      )
                  "
                >
                  <el-option
                    v-for="item in belongingCompanyList"
                    :key="item.belongingCompanyId"
                    :label="item.name"
                    :value="item.belongingCompanyId"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="合同类别" prop="contractCategoryName">
                <el-select
                  v-model="queryParams.contractCategoryName"
                  clearable
                  placeholder="请选择"
                  @change="handleQuery"
                >
                  <el-option
                    v-for="item in contractCategoriesList"
                    :key="item.name"
                    :label="item.name"
                    :value="item.name"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="10">
              <el-form-item
                label="合同失效日期"
                prop="state"
                label-width="108px"
              >
                <el-date-picker
                  v-model="queryParams.value"
                  type="daterange"
                  align="right"
                  unlink-panels
                  :editable="false"
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  @change="handleQuery"
                  format="yyyy-MM-dd"
                  value-format="yyyy-MM-dd"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label="距离到期天数"
                prop="maturityDays"
                label-width="98px"
              >
                <el-input
                  v-model="queryParams.maturityDays"
                  placeholder="请输入到期天数"
                  clearable
                  v-number-input
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item style="float: right">
                <el-button
                  type="primary"
                  icon="el-icon-search"
                  @click="handleQuery"
                  style="width: 85px; height: 40px"
                  >搜索</el-button
                >
                <el-button
                  icon="el-icon-refresh"
                  @click="resetQuery"
                  style="width: 85px; height: 40px"
                  >重置</el-button
                >
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-row>
    </el-card>

    <el-card>
      <stylizedTable
        @reset="getList"
        :selector="false"
        :ordinal="true"
        :queryParams="queryParams"
        :loading="false"
        :total="total"
        title="收款台账管理"
        :tableItem="tableItem"
        :tableData="ExpirationReminderList"
        @getList="getList"
      />
    </el-card>
  </div>
</template>

<script>
import {
  listExpirationReminder,
  getExpirationReminder,
  delExpirationReminder,
  addExpirationReminder,
  updateExpirationReminder,
} from "@/api/contract/ExpirationReminder";
import { listBelongingCompany } from "@/api/contract/BelongingCompany";
import { listContractCategorybyBelongingCompanyId } from "@/api/contract/ContractCategory";

export default {
  name: "ExpirationReminder",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 合同到期提醒表格数据
      ExpirationReminderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        contractId: null,
        maturityDays: null,
        state: null,
        value: [],
      },
      tableItem: [
        {
          porp: "合同名称",
          data: "name",
          fixed: "left",
          width: 300,
        },
        {
          porp: "提醒类型",
          data: "alarmType",
          width: 150,
        },

        {
          porp: "合同编码",
          data: "code",
          width: 200,
        },
        {
          porp: "合同类型",
          data: "type",
        },
        {
          porp: "合同所属公司",
          data: "belongingCompanyName",
          width: 200,
        },
        {
          porp: "合同类别",
          data: "contractCategoryName",
        },
        {
          porp: "收（付）款类型",
          data: "collectionAndPaymentTypes",
          width: 200,
        },
        {
          porp: "收（付）款状态",
          data: "collectionAndPaymentState",
          width: 200,
        },
        {
          porp: "签约方",
          data: "contractPartyList",
          width: 200,
          solt: {
            name: 4,
            property: "contractPartyList",
          },
        },
        {
          porp: "距离到期剩余天数",
          data: "maturityDays",
          width: 150,
        },

        {
          porp: "合同生效日期",
          data: "effectiveTime",
          width: 150,
        },
        {
          porp: "合同终止日期",
          data: "terminationTime",
          width: 150,
        },
        {
          porp: "合同总额（元）",
          data: "allMoney",
          width: 150,
        },
        {
          porp: "未收（付）金额（元）",
          data: "unpaidPayment",
          width: 150,
        },
      ],
      //合同类型
      options: [
        {
          label: "销项合同",
          value: "销项合同",
        },
        {
          label: "进项合同",
          value: "进项合同",
        },
        {
          label: "其他",
          value: "其他",
        },
      ],
      belongingCompanyList: [],
      contractCategoriesList: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        contractId: [
          { required: true, message: "合同id不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getBelongingCompany();
  },
  methods: {
    /** 查询合同到期提醒列表 */
    getList() {
      this.loading = true;
      listExpirationReminder(this.queryParams).then((response) => {
        this.ExpirationReminderList = response.rows;
        console.log(response);
        this.total = response.total;
        this.loading = false;
      });
    },
    //获取公司列表
    getBelongingCompany() {
      listBelongingCompany().then((res) => {
        console.log(res);
        this.belongingCompanyList = res.rows;
      });
    },
    //根据公司id获取合同类别列表
    getlistContractCategorybyBelongingCompanyId(row) {
      listContractCategorybyBelongingCompanyId(row).then((res) => {
        console.log(res);
        this.contractCategoriesList = res.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        expirationReminderId: null,
        contractId: null,
        maturityDays: null,
        state: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      console.log(this.queryParams);
      this.queryParams.takeEffectStart = null;
      this.queryParams.takeEffectEnd = null;
      if (this.queryParams.value) {
        this.queryParams.takeEffectStart = this.queryParams.value[0];
        this.queryParams.takeEffectEnd = this.queryParams.value[1];
      }
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.value = null;
      this.queryParams.takeEffectStart = null;
      this.queryParams.takeEffectEnd = null;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.expirationReminderId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加合同到期提醒";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const expirationReminderId = row.expirationReminderId || this.ids;
      getExpirationReminder(expirationReminderId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改合同到期提醒";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.expirationReminderId != null) {
            updateExpirationReminder(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addExpirationReminder(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const expirationReminderIds = row.expirationReminderId || this.ids;
      this.$modal
        .confirm(
          '是否确认删除合同到期提醒编号为"' +
            expirationReminderIds +
            '"的数据项？'
        )
        .then(function () {
          return delExpirationReminder(expirationReminderIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "contract/ExpirationReminder/export",
        {
          ...this.queryParams,
        },
        `ExpirationReminder_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style scoped>
::v-deep .el-form-item {
  display: flex;
  align-items: center;
}
::v-deep .el-input__inner {
  height: 40px;
}
.no-margin-form * {
  margin: 2px !important;
}
</style>
